package com.visikard.asynctask;

import java.util.List;

import android.os.Handler;
import android.os.Message;

import com.visikard.application.Constant;
import com.visikard.application.UIMessage;
import com.visikard.business.GiftKardBUS;
import com.visikard.item.ItemGiftKard;
import com.visikard.response.BaseResponse;
import com.visikard.response.RingUpMerchantEmailResponse;

public class RingUpMerchantEmailTask extends BaseTask {
	
	public static final int POS_FK_USER_MERCHANT = 0;
	public static final int POS_AMOUNT = 1;
	public static final int POS_EMAIL_OR_CODE = 2;
	
	public RingUpMerchantEmailTask(Handler handler) {
		super(handler);
	}

	public RingUpMerchantEmailTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	@Override
	protected Object doInBackground(Object... params) {

		//Get params
		long fkUserMerchant = (Long)params[POS_FK_USER_MERCHANT];
		double amount = (Double)params[POS_AMOUNT];
		String emailOrCode = (String)params[POS_EMAIL_OR_CODE];
		
		//Validation
		
		//Call service
		GiftKardBUS busObj = new GiftKardBUS();
		return busObj.ringUpMerchantEmail(fkUserMerchant, amount, emailOrCode);
	}

	@Override
	protected boolean handleResult(Object result) {
		RingUpMerchantEmailResponse response = (RingUpMerchantEmailResponse)result;
		if(response != null){
			switch(response.getErrorCode()){
			case BaseResponse.SUCCESS:
				Constant.isNetWorldFail = false;	
				List<ItemGiftKard> listGiftKards = response.getListItemGiftKards();
				if(listGiftKards != null && listGiftKards.size() > 0){
					Message successMsg = new Message();
					successMsg.what = UIMessage.MSG_RINGUP_SUCCESS;
					successMsg.obj = listGiftKards;
					mUIHandler.sendMessage(successMsg);
				}else{
					mUIHandler.sendEmptyMessage(UIMessage.MSG_RINGUP_FAILED);
				}
				return true;
			}
		}
		return false;
	}

}
